// JavaScript Document 30/06
var aErr = new Array('#sUserName_err','#sPassWord1_err','#sConfirm_PassWord1_err','#email_name_err','#sVerifyCode_err','#question_err');
var aNote = new Array('#sUserName_note','#sPassWord1_note','#sConfirm_PassWord1_note','#email_name_note','#sVerifyCode_note','#question_note');
var aTrNote = new Array('#sUserName_tr_note','#sPassWord1_tr_note','#sConfirm_PassWord1_tr_note','#email_name_tr_note','#sVerifyCode_tr_note','#question_tr_note');		
var product_type = $('#product_type').val();
function setValue(obj){
	rId = "#"+$(obj).attr('id')+"_tr";
	labelId = "#"+$(obj).attr('id')+"_label";
	trNoteId = "#"+$(obj).attr('id')+"_tr_note";
	noteId = "#"+$(obj).attr('id')+"_note";
	errId = "#"+$(obj).attr('id')+"_err";
}
function hideNote(cur){	
	for(var i=0;i<aNote.length;i++){
		if(aNote[i]!=cur){
			$(aNote[i]).hide();
			if($(aErr[i]).css('display') == 'none')
				$(aTrNote[i]).removeClass('td_note');		
		}	
	}
}
function onSubmit(){	
	var flag_submit = true;
	if(!checkRequire() || $('#sUserName_err').css('display') != 'none' || $('#sPassWord1_err').css('display') != 'none' 
				|| $('#sConfirm_PassWord1_err').css('display') != 'none'
				|| $('#sVerifyCode_err').css('display') != 'none'
				|| $('#fullname_err').css('display') != 'none' || $('#dName1_err').css('display') != 'none'){
		flag_submit = false;
	}	
	var dd = $("#dName1").val();
	var mm = $("#mName1").val();
	var yyyy = $("#yName1").val();
	if(yyyy != -1 || dd != -1 || mm != -1){
		if(!checkDate(dd,mm,yyyy) ){
			flag_submit = false;
			chkDateErr = 1;
			showError('#dName1',Date_invalid);
		}												  		
	}
	if(flag_submit){
		$('#sUserName').val($('#sUserName_').val());
		//Track GA
		act_pushRegister_GA();
		//END
		return true;
	}
	return false;
}
function checkRequire(){		
	var aInput = new Array('#sUserName','#sPassWord1','#sConfirm_PassWord1','#email_name','#sVerifyCode');
	var count = 0;
	for(var i=0;i<aInput.length;i++){			
		if($(aInput[i]).val() == ''){			
			showMessRequire(aInput[i]);
			count++;
		}
	}
	if(chkAccErr == 1 || chkAccErr == 2 || chkPassErr == 1){
		return false;
	}
	//update support QTP
	chkVerifyCodeErr = 0;
	if(trimString($('#sVerifyCode').val()) != "" ){
		if($('#sVerifyCode').val().length < 6 || chkVerifyCodeErr == 1){
			showError('#sVerifyCode',Verify_Error[0]);
			return false;
		}
	}
	return true;		
}
function showMessRequire(obj){
	setValue(obj);		
	showErrorHTML();
	$(errId).html(Require_Error);	
}
function showErrorHTML(){
	$(noteId).hide();
	$(errId).show();
	$(trNoteId).addClass("td_note");
	$(rId).addClass("tr_error");
}
/******* Neu user co nhap ho ten => thuc hien suggestion ********/
function act_suggestion(){
	var fullname = $('#fullname').val();
	if(trimString(fullname) != ""){
		getAccountName(3);
	}
}
/***** END ****/
function onFocus(obj){
	if(check_show_keyboard()) return true;
	setValue(obj);
	hideNote(noteId);
	if(obj.value == ''){
		$(errId).hide();
		$(rId).removeClass();
		$(labelId).removeClass();		
		$(trNoteId).addClass('td_note');
		if(noteId == '#sPassWord1_note')$(noteId).html(PASWORD_DESCRIPTION);
		$(noteId).show();
		if(trNoteId == '#sConfirm_PassWord1_tr_note'){					
			$(trNoteId).removeClass('td_note');
		}
		if(trNoteId == '#sUserName_tr_note'){	
			
			act_suggestion(); //chua co acc
		}						
	}
	return false;
}
//####################################### Password ##################################################
function checkPersonalId(str){
	if(str.length < 8 || str.length > 15)
		return false;
	if(!checkStringIsNum(str))
		return false;
	return true;
}
function password_level(str){
	if(str.length < 6)
		return false;
	var level = new Array(null,null,null,null);
	for(i=0;i<str.length;i++){		
		// Ky tu thuong
		if(checkCharLow(str.charCodeAt(i)))
			level[0] = 1;
		// Ky tu hoa
		if(checkCharUp(str.charCodeAt(i)))
			level[1] = 1;	
		// Ky tu so
		if(checkNum(str.charCodeAt(i)))
			level[2] = 1;		
	}
	//co ky tu dat biet
	if(!checkCharacter(str)){
		level[3] = 1;
	}
	return level;
}

//####################################### Account Name ##################################################
/*************** Kiem tra ky tu (.,_) o dau va cuoi *****************/
function acc_checkCharBegin(obj){
	if(obj.charAt(0) == '.' || obj.charAt(0) == '_'){
		return false;	
	}	
	return true;
}
/*************** Kiem tra ky tu (.,_) va cuoi *****************/
function acc_checkCharEnd(obj){
	if(obj.charAt(obj.length-1) == '.' || obj.charAt(obj.length-1) == '_'){
		return false;	
	}
	return true;
}
/*************** Kiem tra ky tu gach duoi va dau cham lien tiep *****/
function acc_checkCharContinuous(obj){
	if(obj.indexOf('._') != -1 || obj.indexOf('_.') != -1 || obj.indexOf('__') != -1 || obj.indexOf('..') != -1){
		return false;
	}	
	return true;
}
/*************** Kiem tra khong cho nhap ky tu dat biet *****/
function acc_checkSpecialChars(str){
	for(i=0;i<str.length;i++){
		if((str.charCodeAt(i) < 48 && str.charCodeAt(i) != 46)|| str.charCodeAt(i) > 122)
			return false;
		if(str.charCodeAt(i) > 57 && str.charCodeAt(i) < 65)
			return false;
		if(str.charCodeAt(i) > 90 && str.charCodeAt(i) < 97 && str.charCodeAt(i) != 95)
			return false;		
	}
	return true;
}

function checkVietNam(str){
	for(i=0;i<str.length;i++){
		if(str.charCodeAt(i) < 32 || str.charCodeAt(i) > 126)
			return false;			
	}
	return true;
}
/*********** check len **************/
function acc_checkLen(obj){
	if((obj.length != 0 & obj.length < 6) || obj.length > 32)
		return false;
	return true;
}
/*********** check len **************/
function pass_checkLen(obj){
	if((obj.length != 0 & obj.length < 6) || obj.length > 32)
		return false;
	return true;
}
/******** call suggest account  *******/
function randomstring(word){
	var index = (Math.floor ( Math.random ( ) * arrSuggest.length) );
	return word+arrSuggest[index-1];
}
var cur_acc = '';
var account_list = '';
var cur_w1 = '';
var cur_w2 = '';
var cur_w3 = '';
var chkAccSelect = 0;
function getAccountName(opt,isSuggestion){
	if(opt == 1 && isSuggestion != 1){
		if(checkErrorKeyUp()) return;	
		if(checkErrorBlur()) return;	
	}
	//if($('#sUserName_err').css('display') != 'none') return;	
	var sUserName = $('#sUserName').val();		
	if(sUserName.indexOf(' ') != -1) return false;
	if(opt == 1){		
		if(trimString(sUserName) == '' && !isSuggestion) return;
	}
	if(!isSuggestion)	isSuggestion = 0;
	//disable suggestion
	isSuggestion = 0;

	//Update 20/07
	//if($('#sUserName_err').css('display') != 'none') return false;
	var flag = true;		
	var word1 = '';	
	var word2 = '';	
	var word3 = '';		
	var dd = $("#dName1").val();
	var mm = $("#mName1").val();
	var yyyy = $("#yName1").val();
	if( (yyyy && yyyy != 'Năm') || (dd && dd!='Ngày') || (mm != -1) ){
		if(checkDate(dd,mm,yyyy) ){
			if(dd < 10)	dd = '0'+dd;
			if(mm < 10)	mm = '0'+mm;
			word2 = dd+'/'+mm+'/'+yyyy;
		}												  		
	}	
	//Update 06/07/2009
	var fullName = trimString($('#fullname').val());
	fullName = str_replace_special(fullName);
	if( trimString(fullName) != '' ){
		word1 = fullName;
	}
	//End
	if( $('#sRegionIssue1').val() ){
		word3 = $('#sRegionIssue1').val();
	}
	var sUserName = trimString($('#sUserName').val());
	//neu user ko co, va ko co tu goi y
	if(!sUserName && !word1) return false;
	if(sUserName){
		word1 = sUserName;
	}
	var sessionRegisterPage = $('#sessionRegisterPage').val();
	if(isSuggestion != 1){
		if(trimString(cur_acc) == trimString(sUserName) && trimString(cur_w1) == trimString(word1) && trimString(cur_w2) == trimString(word2) && trimString(cur_w3) == trimString(word3)){
			if(chkAccSelect == 5){
				if(opt != 3){
					showError('#sUserName','Tên đăng nhập đã tồn tại.');
				}
				$('#suggest_account').show(); 
				suggestion_error2();
				return;	
			}
			if(chkAccSelect == 1){
				cur_acc = sUserName;	
				selectAcc(sUserName);
			}else{
				if(trimString(sUserName) == '') return false;
				showError('#sUserName','Tên đăng nhập đã tồn tại.');
				$('#suggest_account').show(); 
				get_more_account();
			}
			return false;
		}
	}
	waiting();
	$.post("/register/check.html", { 
		   'ACC': sUserName, 
		   '1ST': word1, 
		   '2ND': word2, 
		   '3RD': word3,
		   'isSuggesstion': isSuggestion,
		   'sessionRegisterPage': sessionRegisterPage
		 }, 
		function(json){ 			
			cur_acc = sUserName;
			cur_w1 = word1;
			cur_w2 = word2;
			cur_w3 = word3;
			if(json['flag'] == 0){
				chkAccSelect = 0;
				chkAccErr = 2;	
				$('#suggest_account').show(); 
				suggestion_error();	
				return false;
			}
			if(json['flag'] == 1){
				$('#sUserName_').val(sUserName);
				chkAccErr = 0;
				selectAcc(sUserName);					
				$('#sUserName_wait').hide();
				hideError('#sUserName');
				$("#sPassWord1").focus();
				return checkValid("#sUserName_icon");	
			}
			if(json['flag'] == 2){
				chkAccSelect = 0;				
				chkAccErr = 2;
				if(sUserName){
					showError('#sUserName','Tên đăng nhập đã tồn tại.');
				}
				account_list = json['account'];
				get_more_account();
				return false;
			}	
			if(json['flag'] == 3){
				chkAccSelect = 0;
				chkAccErr = 3;	
				if(sUserName){
					showError('#sUserName','Tên đăng nhập đã tồn tại.');
					account_list = '';
				}
				$('#suggest_account').show(); 
				suggestion_error();	
				return false;
			}
			if(json['flag'] == 5){
				chkAccErr = 3;	
				chkAccSelect = 5;
				if(sUserName){
					showError('#sUserName','Tên đăng nhập đã tồn tại.');
				}
				$('#suggest_account').show(); 
				suggestion_error2();	
				return false;
			}
		},'json'
	);	
}
function get_more_account(){
	//disable suggesstion
	$('#suggest_account').hide();
	return false;
	$('#suggest_account').show();
	if(account_list == '' || account_list.length < 2){
		suggestion_error();
		return false;
	}
	var html = "<div style='color:#e05921;font-weight:300'>Một số gợi ý cho tên đăng nhập</div>";
		html += get_html_suggestion(account_list);
	$('#sub_suggest_account').html(html);
}
function suggestion_error(){
	//disable suggesstion
	$('#suggest_account').hide();
	return false;
	$('#suggest_account').show();
	var url_image = $('#url_image_pp4').val();
	var str_error = "<span style='color:#FF0000;font-weight:300'>";
		str_error+= "<img src='"+url_image+"images/iconWarn.gif' align='absmiddle' />";
		str_error+= " Không có gợi ý cho tên đăng nhập.</span>";
	$('#sub_suggest_account').html(str_error);
}
/****** Update ******/
function suggestion_error2(){
	//disable suggesstion
	$('#suggest_account').hide();
	return false;
	$('#suggest_account').show();
	var url_image = $('#url_image_pp4').val();
	var str_error = "<span style='color:#000;font-weight:300;margin-top:5px;margin-bottom:5px'>";	
	str_error+= " <a style='color:#0066cc' onclick='getAccountName(1,1)' href='javascript:void(0)'>Nhấn vào đây</a> để gợi ý tên đăng nhập.</span>";
	$('#sub_suggest_account').html(str_error);
}
/************/
function waiting(){
	//disable suggesstion
	$('#suggest_account').hide();
	return false;
	$('#suggest_account').show();
	var url_image = $('#url_image_pp4').val();
	var str_= "<p><img src='"+url_image+"images/spinner_grey.gif' align='absmiddle' /><br/><br/></p>";
	$('#sub_suggest_account').html(str_);
}
function get_html_suggestion(account){
	var html = '';	
	var index = '';
	var tmp = new Array();
	if(account.length > 1){
		if(account.length > 5){
			var max_ = 5;
		}else{
			var max_ = account.length;	
		}		
		for(var i=0;i<max_;i++){
			var index = i;
			html += '<a onclick="selectAcc(\''+account[index]+'\');act_selectAcc_GA();" href="javascript:void(0)">'+account[index]+'</a><br/>';
		}
	}else{
		var url_image = $('#url_image_pp4').val();
		var html = "<span style='color:#FF0000;font-weight:300'>";
		html+= "<img src='"+url_image+"images/iconWarn.gif' align='absmiddle' />";
		html+= " Không có gợi ý cho tên đăng nhập.</span>";
	}
	return html;
}
function getIndex(account,tmp){
	var index = (Math.floor ( Math.random ( ) * account.length) );
	if(in_array(index,tmp)){
		index = getIndex(account,tmp);
	}
	return index;
}
///////////////////////
function in_array(needle, haystack, strict) {
    var found = false, key, strict = !!strict; 
    for (key in haystack) {
        if ((strict && haystack[key] === needle) || (!strict && haystack[key] == needle)) {
            found = true;
            break;
        }
    } 
    return found;
}
////////////////////
function closeSuggest(){	
	$("#more_suggest").hide();	
	$("#suggest_account").hide();	
	$('#suggest_account').removeClass('goiy_ext');
}	
function selectAcc(acc){
	//set lai current account
	cur_acc = acc;
	//
	chkAccSelect = 1;
	chkAccErr = 0;	
	closeSuggest();
	$('#sUserName').val(acc);
	$('#sUserName_').val(acc);
	var str = acc;
	var strCheck = "<a href='#aaa' onclick=changetext('"+acc+"')>Thay đổi</a>";
	curAcc = str;
	$("#sUserName_readonly").show();	
	$("#sUserName_readonly").html(str);	
	$("#Check_label").html(strCheck);	
	$("#sUserName_input").hide();
	$("#Check_button").hide();
	hideError('#sUserName');
	$("#sPassWord1").focus();	
	return checkValid("#sUserName_icon");	
}
function changetext(acc){
	$("#sUserName_readonly").html('');
	$("#Check_label").html('');	
	$("#sUserName_input").show();
	$("#sUserName").val(acc);
	$("#Check_button").show();		
	$("#sUserName").focus();
}
//#################################################### function expan thong tin ca nhanh
var ick = 0;
function expanInfo(){
	if(!ick){
		$("#tblCMND").show();
		ick = 1;
	}else{
		$("#tblCMND").hide();
		ick = 0;
	}
}
//#################################################### general
function checkValid(obj){
	$(obj).show();
	$(obj).fadeOut(2000);
	return true;
	
}
/************ End ************/
// show error
function showError(obj,mess){
	setValue(obj);
	$(errId).html(mess);
	showErrorHTML()
	return true;
}
//Hide Error
function hideError(obj){
	setValue(obj);		
	$(rId).removeClass();
	$(labelId).removeClass();
	$(trNoteId).removeClass();
	//$(errId).fadeOut("slow");	
	$(errId).hide();	
}
//#################################################### Image security
// Xu ly verify image
var chkVerifyCodeErr = 1;
var chkVerify = 0;
function refreshImage(){
	//if(chkVerify == 1)
		//return false;
	var datetime = new Date();
	var url = '/imagesec/index.'+datetime.getTime()+'.html';
	$("#sVerifyImg").attr({ src: url});
}
var is_valid = 0;
var cur_verify = '';
function checkImage_(){
	if(is_valid == 1 || cur_verify == $('#sVerifyCode').val())
		return false;
	if(!$('#sVerifyCode').val())
		return false;	
	if($('#sVerifyCode').val().length < 6){
		chkVerifyCodeErr = 1;		
		showError('#sVerifyCode',Verify_Error[0])	
		return false;
	}
	if($('#sVerifyCode').val().length > 6)
		return false;
	$('#sVerifyCode_wait').show();
	$('#sVerifyCode_note').hide();
	chkVerifyCodeErr = 0;
	return true;	
}
///////////////////// General ////////////////////////
function checkDate(dd,mm,yyyy){     
    var mydate=new Date();
	var year=mydate.getFullYear();
	var month=mydate.getMonth() + 1;
	var daym=mydate.getDate(); 
	//alert(dd+' --- '+ daym);
	//alert(mm+' --- '+ month);
	//alert(yyyy+' --- '+ year);
   //Kiem tra thoi gian hien tai
   if(!checkStringIsNum(dd) || !checkStringIsNum(mm) || !checkStringIsNum(yyyy))   
	   	return false;
   if(dd < 1 || dd > 31 || yyyy < 1900)
	   	return false;
   if(yyyy > year)
   		return false;  
   if(yyyy == year){
   		if(mm > month)
			return false;  
		if(mm == month)
			if(dd > daym || dd == daym)
				return false;   
   }   
   //Kiem tra hop le
   if(!isNaN(yyyy)&&(yyyy!="")&&(yyyy<10000)){     
		if ((mm==4 || mm==6 || mm==9 || mm==11) && dd==31)
			return false; 
		if (mm == 2) { // check for february 29th
			var isleap = (yyyy % 4 == 0 && (yyyy % 100 != 0 || yyyy % 400 == 0));
			if (dd>29 || (dd==29 && !isleap))
				return false; 
		}
   }   
   return true;
}
//////////////////////
var is_valid = 0;
var cur_verify = '';
function checkImage(){
	if(!$('#sVerifyCode').val())
		return false;	
	if($('#sVerifyCode').val().length < 6){
		chkVerifyCodeErr = 1;				
		showError('#sVerifyCode',Verify_Error[1])	
		return false;
	}
	if($('#sVerifyCode').val().length > 6)
		return false;
	//
	is_valid = 1;
	chkVerifyCodeErr = 0;
	hideError('#sVerifyCode');
	chkVerify = 1;
	checkValid("#sVerifyCode_icon");	
	$('#sVerifyCode_note').hide();
	return true;
	//		
	$('#sVerifyCode_wait').show();
	var url = '/checkimage/index.html';
	$.post(url, {sVerifyCode:$('#sVerifyCode').val()}, 
	  function(data){
		$('#sVerifyCode_wait').hide();
		if(data){
			is_valid = 1;
			chkVerifyCodeErr = 0;
			hideError('#sVerifyCode');
			chkVerify = 1;
			checkValid("#sVerifyCode_icon");	
			$('#sVerifyCode_note').hide();
			return true;
		}else{
			cur_verify = $('#sVerifyCode').val();
			chkVerifyCodeErr = 1;
			//$('#sVerifyCode_wait').hide();
			showError('#sVerifyCode',Verify_Error[0]);
			return false;
		}
	  } 
	);	
}
////////////////////////////// Update Web register /////////////////
$("#fullname").focus( function() {	
	if(this.value == '') hideError( $("#fullname"));
} );
$("#dName1").focus( function() {	
	if(this.value == '') hideError( $("#dName1"));
} );
$("#sRegionIssue1").focus( function() {	
	if(this.value == '-1') hideError( $("#sRegionIssue1"));
} );
/* #################################### xu ly date #############################*/
var Date_invalid = 'Ngày sinh không hợp lệ.';
$("#yName1").blur( function() {
	return checkDateValid('yyyy');
} );
$("#dName1").blur( function() {
	return checkDateValid('dd');
} );
$("#mName1").blur( function() {
	return checkDateValid('mm');
} );
function checkDateValid(opt){
	var flag = 0;
	var dd = trimString($("#dName1").val());	
	var mm = $("#mName1").val();
	var yyyy = trimString($("#yName1").val());
	if(dd == -1 && mm==-1 && yyyy == -1)	hideError('#dName1');
	//neu blur ddd
	if(opt == 'dd'){
		if(mm != -1 && yyyy != -1){
			if(!checkDate(dd,mm,yyyy) ){
				return showError('#dName1',Date_invalid);
			}else{
				flag = 1;	
			}		
		}	
	}
	//neu blur mm
	if(opt == 'mm'){
		if(yyyy != -1){
			if(!checkDate(dd,mm,yyyy) ){
				return showError('#dName1',Date_invalid);
			}else{
				flag = 1;	
			}
		}	
	}
	//neu blur yyyy
	if(opt == 'yyyy'){
		if(dd != -1 || mm != -1){
			if(!checkDate(dd,mm,yyyy) ){
				return showError('#dName1',Date_invalid);
			}else{
				flag = 1;				
			}			
		}	
	}
	if(flag == 1){
		hideError('#dName1');
		return checkValid("#dName1_icon");
	}
}
//#################
$("#sRegionIssue1").blur( function() {			
	if(this.value != -1){
		hideError('#sRegionIssue1');			
		return checkValid("#sRegionIssue1_icon");	
	}
} );
function countWords(str){
	var str_ = trimString(str);
	return str_.split(' ').length;
}
function trimString(text)
{
	var len=text.length;
	var i=0;
	var j=len-1;
	var s="";
	
	while(text.charAt(i)==" ")
		i++;
	
	while(text.charAt(j)==" ")
		j--;
	
	if(i>j) 
		s="";
	else 
		s=text.substring(i,j+1);
	
	return s;
}
//update 14/04
function check_date_submit(){
	var dd = $("#dName1").val();
	var mm = $("#mName1").val();
	var yyyy = $("#yName1").val();
	if(yyyy != 'Năm'){
		if(yyyy < 1900){
			return showError('#dName1',Date_invalid);
		}
	}	
	if(!checkDate(dd,mm,yyyy)){
		return showError('#dName1',Date_invalid);
	}
}
//Update 06/07/2009
function str_replace_special(subject){
	var arrSpecialFrom= new Array(
		"!","\"","#","$","$","&","'","(",")","*","+","'","-",".","/",":",";","<",">","?","@","[","\\","]","^","_","`","{","|","}","~","%","=","  "
	);
	var arrSpecialEnd= new Array(
		"","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","",""
	);		
	return str_replace(arrSpecialFrom,arrSpecialEnd, subject);
}
function str_replace(search, replace, subject, count) {
    // http://kevin.vanzonneveld.net
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Gabriel Paderni
    // +   improved by: Philip Peterson
    // +   improved by: Simon Willison (http://simonwillison.net)
    // +    revised by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)
    // +   bugfixed by: Anton Ongson
    // +      input by: Onno Marsman
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +    tweaked by: Onno Marsman
    // +      input by: Brett Zamir (http://brett-zamir.me)
    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   input by: Oleg Eremeev
    // +   improved by: Brett Zamir (http://brett-zamir.me)
    // +   bugfixed by: Oleg Eremeev
    // %          note 1: The count parameter must be passed as a string in order
    // %          note 1:  to find a global variable in which the result will be given
    // *     example 1: str_replace(' ', '.', 'Kevin van Zonneveld');
    // *     returns 1: 'Kevin.van.Zonneveld'
    // *     example 2: str_replace(['{name}', 'l'], ['hello', 'm'], '{name}, lars');
    // *     returns 2: 'hemmo, mars'
 
    var i = 0, j = 0, temp = '', repl = '', sl = 0, fl = 0,
            f = [].concat(search),
            r = [].concat(replace),
            s = subject,
            ra = r instanceof Array, sa = s instanceof Array;
    s = [].concat(s);
    if (count) {
        this.window[count] = 0;
    }
 
    for (i=0, sl=s.length; i < sl; i++) {
        if (s[i] === '') {
            continue;
        }
        for (j=0, fl=f.length; j < fl; j++) {
            temp = s[i]+'';
            repl = ra ? (r[j] !== undefined ? r[j] : '') : r[0];
            s[i] = (temp).split(f[j]).join(repl);
            if (count && s[i] !== temp) {
                this.window[count] += (temp.length-s[i].length)/f[j].length;}
        }
    }
    return sa ? s : s[0];
}